﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Entity;

namespace CafeTek
{
    public partial class FrmMain : Form
    {
        public static int tableId = -1;
        public static int BillId = -1;
        public static Table Gtable;
        public int TableIdisMoved = -1; //table move
        public Bill Gbill = new Bill();
        public BillDetailDB _BillDetailDb = new BillDetailDB();
        Employee Gemp = new Employee();
        private TableDB tableDb = null;
        private IList<Table> _listtable;
        public TableDB _tableDb = null;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void iToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Load status all tables add to listview
        /// </summary>
        public void LoadStatusTable()
        {
            tableDb = new TableDB();
            _listtable = new List<Table>();

            _listtable = tableDb.GetALl();

            //clear all items on listview
            lstvTable.Items.Clear();

            foreach (Table itemtable in _listtable)
            {
                ListViewItem _item = new ListViewItem(itemtable.Name);
                _item.Tag = itemtable; ;
                int imageindex; //status table

                if (itemtable.Status == (int)BillStatus.Open) //Table Open
                    imageindex = 0;
                else
                    if (itemtable.Status == (int)BillStatus.Payed) //Table Payed
                    {
                        imageindex = 1;
                    }
                    else //table Close
                        imageindex = 2;

                //if table to move
                if (itemtable.TableId == TableIdisMoved && itemtable.Status == (int)BillStatus.Open)
                    imageindex = 3;

                _item.ImageIndex = imageindex;
                
                lstvTable.Items.Add(_item);
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            LoadStatusTable();

            Gemp = frmLogin._emp;            
        }

        public void setEnable(bool value)
        {
            paymentItem.Enabled = value;
            joinItem.Enabled = value;
            splitItem.Enabled = value;
            movingItem.Enabled = value;
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            //click right mouse on table
            if (e.Button == MouseButtons.Right)
            {
                if (lstvTable.SelectedItems.Count == 1)
                {
                    Table table = new Table();
                    table = (Table)(lstvTable.SelectedItems[0].Tag);
                    tableId = table.TableId; ;

                    Gtable = new Table();
                    Gtable.TableId = tableId;
                    Gtable.Name = lstvTable.SelectedItems[0].Text;

                    Gbill = _BillDetailDb.GetBillFromTableId(tableId);
                    int imageindex = lstvTable.SelectedItems[0].ImageIndex;

                    if (imageindex == 0 || imageindex == 3)
                    {
                        setEnable(true);
                        toItem.Enabled = false;
                        movingItem.Enabled = (tableId == TableIdisMoved) ? false : true;

                    }
                    else
                    {
                        setEnable(false);
                        toItem.Enabled = (TableIdisMoved == -1) ? false : true;
                    }
                    try
                    {
                        for (int i = 0; i < lstvTable.FocusedItem.SubItems.Count; i++)
                            if (lstvTable.FocusedItem.SubItems[0].Text != "")
                            {

                                ctm.Visible = true;
                                lstvTable.ContextMenuStrip = ctm;
                                break;
                            }
                            else
                                lstvTable.ContextMenuStrip = ctm;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    if (lstvTable.ContextMenuStrip != null)
                    {
                        lstvTable.ContextMenuStrip.Close();
                        lstvTable.ContextMenuStrip = null;
                    }
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Gbill = _BillDetailDb.GetBillFromTableId(FrmMain.tableId);
        }

        /// <summary>
        /// Move all product of table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void movingItem_Click(object sender, EventArgs e)
        {
            TableIdisMoved = tableId; //table choose
            lstvTable.SelectedItems[0].ImageIndex = 3; //set imageindex is idle
            LoadStatusTable(); //reLoad status table
        }

        //when doubleClick or choose Open then Open table
        public void OpenTable()
        {
            if (lstvTable.SelectedItems.Count == 0)
                return;

            Table table = new Table();
            table = (Table)(lstvTable.SelectedItems[0].Tag);

            tableId =table.TableId;
            Gtable = new Table();
            Gtable.TableId = tableId;
            Gtable.Name = lstvTable.SelectedItems[0].Text;

            FrmSelling frm = new FrmSelling();
            frm.ShowDialog();
        }

        private void lstvTable_DoubleClick(object sender, EventArgs e)
        {
            OpenTable();
        }

        private void FrmMain_Activated(object sender, EventArgs e)
        {
            LoadStatusTable();
        }

        /// <summary>
        /// Close table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tableId == -1)
            {
                return;
            }
            _tableDb = new TableDB();
            string message = _tableDb.CLoseTheTable(tableId);

            if (message != "")
            {
                MessageBox.Show(message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            LoadStatusTable();
        }
                
        /// <summary>
        /// Split table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void splitItem_Click(object sender, EventArgs e)
        {
            SplitTable frm = new SplitTable();
            frm.Show();
        }

        /// <summary>
        /// move all product to table choose
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toItem_Click(object sender, EventArgs e)
        {
            BillDB _billdb = new BillDB();
            //to table selected
            _billdb.movetable(TableIdisMoved, tableId, Gemp.EmployeeId);

            _tableDb = new TableDB();
            //Close table is moved
            _tableDb.CLoseTheTable(TableIdisMoved);
            TableIdisMoved = -1;
            LoadStatusTable();//ReLoad status tableB
        }

        /// <summary>
        /// Open table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openitem_Click(object sender, EventArgs e)
        {
            OpenTable();
        }

        /// <summary>
        /// Join table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void joinItem_Click(object sender, EventArgs e)
        {
            BillDetailDB billDetail = new BillDetailDB();
            Bill bill = new Bill();
            bill = billDetail.GetBillFromTableId(tableId);
            FrmJoinTable f = new FrmJoinTable(bill.BillId);
            f.ShowDialog();
        }

        private void productsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmListProduct frm = new FrmListProduct();
            frm.ShowDialog();
        }

        private void productTypeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmListProductType frm = new FrmListProductType();
            frm.ShowDialog();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        //Seling
        private void sellingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmStatistic frm = new FrmStatistic();
            frm.ShowDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Nguyễn Đức Chính - TinK31A");
        }

        /// <summary>
        /// Payment
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paymentItem_Click(object sender, EventArgs e)
        {
            FrmPrintPay frm = new FrmPrintPay();
            frm.printPay(_BillDetailDb.GetDataviewPrint(Gbill));
            frm.Show();
            _BillDetailDb.PayTheBill(Gbill);
        }

    }
}
